<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no"><title>Z | SunMoon Space</title><meta name="keywords" content="模板"><meta name="author" content="Sun Moon"><meta name="copyright" content="Sun Moon"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="第一题[图片来源于网上]  思路代码  第二题（迷宫问题[图片来源于网上]   思路第一题bfs，找到所有可以到达的点，总的减可达的数量第二题dfs，从奖品点出发，dfs找到出口，取最短路 代码从一个入口，得到所有可到的点，放在set集合中。 &#x2F;&#x2F;最终结果中，set保存的是入口可以到达的所有顶点 public int getOut(char[][] maze)&amp;#123;        int n">
<meta property="og:type" content="article">
<meta property="og:title" content="Z">
<meta property="og:url" content="https://www.sunmoon.site/2020/01/29/%E6%9C%BA%E8%80%83/%E5%AD%97%E8%8A%82/index.html">
<meta property="og:site_name" content="SunMoon Space">
<meta property="og:description" content="第一题[图片来源于网上]  思路代码  第二题（迷宫问题[图片来源于网上]   思路第一题bfs，找到所有可以到达的点，总的减可达的数量第二题dfs，从奖品点出发，dfs找到出口，取最短路 代码从一个入口，得到所有可到的点，放在set集合中。 &#x2F;&#x2F;最终结果中，set保存的是入口可以到达的所有顶点 public int getOut(char[][] maze)&amp;#123;        int n">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://s1.ax1x.com/2022/05/19/OHAFsK.md.jpg">
<meta property="article:published_time" content="2020-01-28T16:37:00.000Z">
<meta property="article:modified_time" content="2022-09-17T00:44:58.756Z">
<meta property="article:author" content="Sun Moon">
<meta property="article:tag" content="模板">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://s1.ax1x.com/2022/05/19/OHAFsK.md.jpg"><link rel="shortcut icon" href="/img/avatar.jpg"><link rel="canonical" href="https://www.sunmoon.site/2020/01/29/%E6%9C%BA%E8%80%83/%E5%AD%97%E8%8A%82/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"簡"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":200},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: {"limitCount":50,"languages":{"author":"作者: Sun Moon","link":"链接: ","source":"来源: SunMoon Space","info":"著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"}},
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/flickr-justified-gallery@2/dist/fjGallery.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Z',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-09-17 08:44:58'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const detectApple = () => {
      if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/css/backgound.css"><meta name="generator" content="Hexo 5.4.2"></head><body><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="/img/avatar.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data is-center"><div class="data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">43</div></a></div><div class="data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a></div><div class="data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 链接</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></li><li><a class="site-page child" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/aplayer/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://s1.ax1x.com/2022/05/19/OHAFsK.md.jpg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">SunMoon Space</a></span><div id="he-plugin-simple"></div><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 主页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> Archives</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page group" href="javascript:void(0);"><i class="fa-fw fas fa-list"></i><span> 链接</span><i class="fas fa-chevron-down"></i></a><ul class="menus_item_child"><li><a class="site-page child" href="/link/"><i class="fa-fw fas fa-link"></i><span> Link</span></a></li><li><a class="site-page child" href="/about/"><i class="fa-fw fas fa-heart"></i><span> About</span></a></li></ul></div><div class="menus_item"><a class="site-page" href="/aplayer/"><i class="fa-fw fas fa-music"></i><span> 音乐</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Z</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2020-01-28T16:37:00.000Z" title="发表于 2020-01-29 00:37:00">2020-01-29</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-09-17T00:44:58.756Z" title="更新于 2022-09-17 08:44:58">2022-09-17</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E6%A3%80%E9%AA%8C/">检验</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Z"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="第一题"><a href="#第一题" class="headerlink" title="第一题"></a>第一题</h1><p>[图片来源于网上]</p>
<p><a target="_blank" rel="noopener" href="https://imgse.com/i/vyzSk6"><img src="https://s1.ax1x.com/2022/08/22/vyzSk6.jpg" alt="vyzSk6.jpg"></a><br><a target="_blank" rel="noopener" href="https://imgse.com/i/vyxxTx"><img src="https://s1.ax1x.com/2022/08/22/vyxxTx.jpg" alt="vyxxTx.jpg"></a></p>
<h2 id="思路"><a href="#思路" class="headerlink" title="思路"></a>思路</h2><h2 id="代码"><a href="#代码" class="headerlink" title="代码"></a>代码</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"></span><br></pre></td></tr></table></figure>

<h1 id="第二题（迷宫问题"><a href="#第二题（迷宫问题" class="headerlink" title="第二题（迷宫问题"></a>第二题（迷宫问题</h1><p>[图片来源于网上]</p>
<p><a target="_blank" rel="noopener" href="https://imgse.com/i/vyxOX9"><img src="https://s1.ax1x.com/2022/08/22/vyxOX9.jpg" alt="vyxOX9.jpg"></a><br><a target="_blank" rel="noopener" href="https://imgse.com/i/vyxql4"><img src="https://s1.ax1x.com/2022/08/22/vyxql4.jpg" alt="vyxql4.jpg"></a></p>
<p><a target="_blank" rel="noopener" href="https://imgse.com/i/v6Af81"><img src="https://s1.ax1x.com/2022/08/22/v6Af81.md.jpg" alt="v6Af81.md.jpg"></a></p>
<h2 id="思路-1"><a href="#思路-1" class="headerlink" title="思路"></a>思路</h2><p>第一题bfs，找到所有可以到达的点，总的减可达的数量<br>第二题dfs，从奖品点出发，dfs找到出口，取最短路</p>
<h2 id="代码-1"><a href="#代码-1" class="headerlink" title="代码"></a>代码</h2><p>从一个入口，得到所有可到的点，放在set集合中。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//最终结果中，set保存的是入口可以到达的所有顶点</span></span><br><span class="line"> <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">getOut</span><span class="params">(<span class="type">char</span>[][] maze)</span>&#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">n</span> <span class="operator">=</span> maze.length;</span><br><span class="line">        <span class="type">int</span> <span class="variable">m</span> <span class="operator">=</span> maze[<span class="number">0</span>].length;</span><br><span class="line">        <span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> <span class="number">0</span>, y = <span class="number">0</span>;</span><br><span class="line">        k1: <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>; i&lt;n; i++)&#123;</span><br><span class="line">            k2: <span class="keyword">for</span> (<span class="type">int</span> j=<span class="number">0</span>; j&lt;m; j++)&#123;</span><br><span class="line">                <span class="keyword">if</span> (maze[i][j] == <span class="string">&#x27;o&#x27;</span>)&#123;</span><br><span class="line">                    x = i;</span><br><span class="line">                    y = j;</span><br><span class="line">                    <span class="keyword">break</span> k1;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//找到唯一入口</span></span><br><span class="line">        <span class="type">Point</span> <span class="variable">point</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Point</span>(x, y);</span><br><span class="line">        dq.offer(point);</span><br><span class="line">        set.add(point);</span><br><span class="line">        <span class="keyword">while</span> (dq.size() != <span class="number">0</span>)&#123;</span><br><span class="line">            <span class="type">Point</span> <span class="variable">poll</span> <span class="operator">=</span> dq.poll();</span><br><span class="line">            <span class="type">int</span> <span class="variable">pollx</span> <span class="operator">=</span> poll.x, polly = poll.y;</span><br><span class="line">            <span class="type">int</span>[][] directions = <span class="keyword">new</span> <span class="title class_">int</span>[][]&#123;&#123;<span class="number">1</span>,<span class="number">0</span>&#125;,&#123;<span class="number">0</span>,<span class="number">1</span>&#125;,&#123;-<span class="number">1</span>,<span class="number">0</span>&#125;,&#123;<span class="number">0</span>,-<span class="number">1</span>&#125;&#125;;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span>[] dir : directions) &#123;</span><br><span class="line">                <span class="type">int</span> <span class="variable">newX</span> <span class="operator">=</span> pollx+dir[<span class="number">0</span>];</span><br><span class="line">                <span class="type">int</span> <span class="variable">newY</span> <span class="operator">=</span> polly+dir[<span class="number">1</span>];</span><br><span class="line">                <span class="keyword">if</span> (isOk(maze,pollx,polly,newX,newY))&#123;</span><br><span class="line">                    <span class="type">Point</span> <span class="variable">point1</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Point</span>(newX, newY);</span><br><span class="line">                    <span class="type">boolean</span> <span class="variable">b</span> <span class="operator">=</span> set.add(point1);</span><br><span class="line">                    <span class="keyword">if</span> (b) dq.offer(point1);</span><br><span class="line">                &#125;</span><br><span class="line"></span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> m*n- set.size();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">boolean</span> <span class="title function_">isOk</span><span class="params">(<span class="type">char</span>[][] maze, <span class="type">int</span> x, <span class="type">int</span> y, <span class="type">int</span> nx, <span class="type">int</span> ny)</span>&#123;</span><br><span class="line">        <span class="keyword">if</span> (nx&lt;<span class="number">0</span> || nx &gt;= maze.length || ny&lt;<span class="number">0</span> || ny&gt;=maze[<span class="number">0</span>].length )&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">if</span> ((maze[nx][ny] == <span class="string">&#x27;.&#x27;</span>) || (x == nx-<span class="number">1</span> &amp;&amp; maze[nx][ny] == <span class="string">&#x27;u&#x27;</span>) || (x == nx+<span class="number">1</span> &amp;&amp; maze[nx][ny] == <span class="string">&#x27;d&#x27;</span>) || (y == ny+<span class="number">1</span> &amp;&amp; maze[nx][ny] == <span class="string">&#x27;r&#x27;</span>) || (y == ny-<span class="number">1</span> &amp;&amp; maze[nx][ny] == <span class="string">&#x27;l&#x27;</span>))&#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">class</span> <span class="title class_">Point</span>&#123;</span><br><span class="line">        <span class="type">int</span> x;</span><br><span class="line">        <span class="type">int</span> y;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">public</span> <span class="title function_">Point</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span> &#123;</span><br><span class="line">            <span class="built_in">this</span>.x = x;</span><br><span class="line">            <span class="built_in">this</span>.y = y;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object o)</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">this</span> == o) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">if</span> (o == <span class="literal">null</span> || getClass() != o.getClass()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            <span class="type">Point</span> <span class="variable">point</span> <span class="operator">=</span> (Point) o;</span><br><span class="line">            <span class="type">return</span> <span class="variable">x</span> <span class="operator">=</span>= point.x &amp;&amp;</span><br><span class="line">                    y == point.y;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">hashCode</span><span class="params">()</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> Objects.hash(x, y);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;Point&#123;&quot;</span> +</span><br><span class="line">                    <span class="string">&quot;x=&quot;</span> + x +</span><br><span class="line">                    <span class="string">&quot;, y=&quot;</span> + y +</span><br><span class="line">                    <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>从内部一个顶点，到外面的所有出路，存在res中；但本题求的是最短路径；返回最小的即可</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">private</span> LinkedList&lt;Point&gt; path = <span class="keyword">new</span> <span class="title class_">LinkedList</span>&lt;&gt;();</span><br><span class="line">    <span class="keyword">private</span> ArrayList&lt;List&lt;Point&gt;&gt; res = <span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;();</span><br><span class="line">    <span class="keyword">public</span> List&lt;Point&gt; <span class="title function_">getMin</span><span class="params">(<span class="type">int</span>[][] maze)</span>&#123;</span><br><span class="line">        <span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> <span class="number">0</span>,y = <span class="number">0</span>;</span><br><span class="line">        k1: <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; maze.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; maze[<span class="number">0</span>].length; j++) &#123;</span><br><span class="line">                <span class="keyword">if</span> (maze[i][j] == <span class="number">8</span>)&#123;</span><br><span class="line">                    x = i;</span><br><span class="line">                    y = j;</span><br><span class="line">                    <span class="keyword">break</span> k1;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="comment">//找到宝藏点，从这一点开始寻找出口</span></span><br><span class="line">        dfs(maze,x,y);</span><br><span class="line">        <span class="type">int</span> <span class="variable">index</span> <span class="operator">=</span> <span class="number">0</span>;</span><br><span class="line">        <span class="type">int</span> <span class="variable">l</span> <span class="operator">=</span> Integer.MAX_VALUE;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; res.size(); i++) &#123;</span><br><span class="line">            <span class="keyword">if</span> (res.get(i).size() &lt; l)&#123;</span><br><span class="line">                l = res.get(i).size();</span><br><span class="line">                index = i;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> res.get(index);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">void</span> <span class="title function_">dfs</span><span class="params">(<span class="type">int</span>[][] maze, <span class="type">int</span> x, <span class="type">int</span> y)</span>&#123;</span><br><span class="line">        <span class="type">Point</span> <span class="variable">point</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Point</span>(x, y);</span><br><span class="line">        path.add(point);</span><br><span class="line">        <span class="keyword">if</span> (isExit(maze,x,y))&#123;</span><br><span class="line">            res.add(<span class="keyword">new</span> <span class="title class_">ArrayList</span>&lt;&gt;(path));</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="type">int</span>[][] directions = <span class="keyword">new</span> <span class="title class_">int</span>[][]&#123;&#123;<span class="number">1</span>,<span class="number">0</span>&#125;,&#123;<span class="number">0</span>,<span class="number">1</span>&#125;,&#123;-<span class="number">1</span>,<span class="number">0</span>&#125;,&#123;<span class="number">0</span>,-<span class="number">1</span>&#125;&#125;;</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span>[] dir : directions) &#123;</span><br><span class="line">            <span class="type">int</span> <span class="variable">newX</span> <span class="operator">=</span> x+dir[<span class="number">0</span>];</span><br><span class="line">            <span class="type">int</span> <span class="variable">newY</span> <span class="operator">=</span> y+dir[<span class="number">1</span>];</span><br><span class="line">            <span class="comment">//这个if其实定义了递归的终止条件</span></span><br><span class="line">            <span class="keyword">if</span> (!path.contains(<span class="keyword">new</span> <span class="title class_">Point</span>(newX,newY)) &amp;&amp; isOk(maze,newX,newY))&#123;</span><br><span class="line">                dfs(maze,newX,newY);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"><span class="comment">//回溯</span></span><br><span class="line">        path.removeLast();</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">boolean</span> <span class="title function_">isOk</span><span class="params">(<span class="type">int</span>[][] maze, <span class="type">int</span> x, <span class="type">int</span> y)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> x &lt; maze.length &amp;&amp; x &gt;= <span class="number">0</span> &amp;&amp; y &gt;=<span class="number">0</span> &amp;&amp; y &lt; maze[<span class="number">0</span>].length &amp;&amp; (maze[x][y] == <span class="number">0</span> || maze[x][y] == <span class="number">8</span>);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">private</span> <span class="type">boolean</span> <span class="title function_">isExit</span><span class="params">(<span class="type">int</span>[][] maze, <span class="type">int</span> x, <span class="type">int</span> y)</span>&#123;</span><br><span class="line">        <span class="keyword">return</span> (x == <span class="number">0</span> || x == maze.length-<span class="number">1</span> || y == <span class="number">0</span> || y == maze[<span class="number">0</span>].length) &amp;&amp; maze[x][y] != <span class="number">1</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">class</span> <span class="title class_">Point</span>&#123;</span><br><span class="line">        <span class="type">int</span> x;</span><br><span class="line">        <span class="type">int</span> y;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">public</span> <span class="title function_">Point</span><span class="params">(<span class="type">int</span> x, <span class="type">int</span> y)</span> &#123;</span><br><span class="line">            <span class="built_in">this</span>.x = x;</span><br><span class="line">            <span class="built_in">this</span>.y = y;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">public</span> <span class="title function_">Point</span><span class="params">()</span> &#123;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">equals</span><span class="params">(Object o)</span> &#123;</span><br><span class="line">            <span class="keyword">if</span> (<span class="built_in">this</span> == o) <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">            <span class="keyword">if</span> (o == <span class="literal">null</span> || getClass() != o.getClass()) <span class="keyword">return</span> <span class="literal">false</span>;</span><br><span class="line">            <span class="type">Point</span> <span class="variable">point</span> <span class="operator">=</span> (Point) o;</span><br><span class="line">            <span class="type">return</span> <span class="variable">x</span> <span class="operator">=</span>= point.x &amp;&amp;</span><br><span class="line">                    y == point.y;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> <span class="type">int</span> <span class="title function_">hashCode</span><span class="params">()</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> Objects.hash(x, y);</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span> &#123;</span><br><span class="line">            <span class="keyword">return</span> <span class="string">&quot;Point&#123;&quot;</span> +</span><br><span class="line">                    <span class="string">&quot;x=&quot;</span> + x +</span><br><span class="line">                    <span class="string">&quot;, y=&quot;</span> + y +</span><br><span class="line">                    <span class="string">&#x27;&#125;&#x27;</span>;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<h1 id="第三题"><a href="#第三题" class="headerlink" title="第三题"></a>第三题</h1><p>[图片来源于网上]</p>
<p><a target="_blank" rel="noopener" href="https://imgse.com/i/vyxjmR"><img src="https://s1.ax1x.com/2022/08/22/vyxjmR.jpg" alt="vyxjmR.jpg"></a><br><a target="_blank" rel="noopener" href="https://imgse.com/i/vyxL6J"><img src="https://s1.ax1x.com/2022/08/22/vyxL6J.jpg" alt="vyxL6J.jpg"></a></p>
<h1 id="第四题"><a href="#第四题" class="headerlink" title="第四题"></a>第四题</h1><p>[图片来源于网上]</p>
<p><a target="_blank" rel="noopener" href="https://imgse.com/i/vyxv01"><img src="https://s1.ax1x.com/2022/08/22/vyxv01.jpg" alt="vyxv01.jpg"></a><br><a target="_blank" rel="noopener" href="https://imgse.com/i/vyzptK"><img src="https://s1.ax1x.com/2022/08/22/vyzptK.jpg" alt="vyzptK.jpg"></a></p>
<h1 id="第一题（员工考试）"><a href="#第一题（员工考试）" class="headerlink" title="第一题（员工考试）"></a>第一题（员工考试）</h1><h2 id="思路（暴力搜索）"><a href="#思路（暴力搜索）" class="headerlink" title="思路（暴力搜索）"></a>思路（暴力搜索）</h2><ul>
<li>用数组temp记录0-100存在的情况。</li>
<li>分3种可能（错1道，2道，3道），计算出分数，对应分数的temp+1；</li>
</ul>
<h2 id="代码-2"><a href="#代码-2" class="headerlink" title="代码"></a>代码</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">        <span class="type">int</span> <span class="variable">n</span> <span class="operator">=</span> sc.nextInt();</span><br><span class="line">        <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[]&#123;<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">2</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">4</span>,<span class="number">8</span>,<span class="number">8</span>,<span class="number">8</span>,<span class="number">8</span>,<span class="number">8</span>&#125;;</span><br><span class="line">        <span class="comment">//sum[i]表示i道及i道以前都做对的分数</span></span><br><span class="line">        <span class="type">int</span>[] sum = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">26</span>];</span><br><span class="line">        <span class="type">int</span>[] temp = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">101</span>];</span><br><span class="line">        temp[<span class="number">100</span>] = <span class="number">1</span>;</span><br><span class="line">        <span class="type">int</span> i,j,k;</span><br><span class="line">        <span class="keyword">for</span> (i=<span class="number">1</span>; i&lt;<span class="number">26</span>;i++)&#123;</span><br><span class="line">            sum[i] = sum[i-<span class="number">1</span>] + arr[i-<span class="number">1</span>];</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//错一道题</span></span><br><span class="line">        <span class="keyword">for</span> (i = <span class="number">0</span>; i&lt;<span class="number">25</span>; i++)&#123;</span><br><span class="line">            temp[<span class="number">100</span>-arr[i]]++;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//错两道题</span></span><br><span class="line">        <span class="keyword">for</span> (i=<span class="number">0</span>; i&lt;<span class="number">24</span>; i++)&#123;</span><br><span class="line">            <span class="keyword">for</span> (j = i+<span class="number">1</span>; j&lt;<span class="number">25</span>; j++)&#123;</span><br><span class="line">                temp[<span class="number">100</span>-arr[i] - arr[j]]++;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="comment">//错三道题</span></span><br><span class="line">        <span class="comment">//确定第3道的位置</span></span><br><span class="line">        <span class="keyword">for</span> (k=<span class="number">2</span>;k&lt;<span class="number">25</span>;k++)&#123;</span><br><span class="line">            <span class="comment">//确定第1道的位置</span></span><br><span class="line">            <span class="keyword">for</span> (i=<span class="number">0</span>;i&lt;=k-<span class="number">2</span>;i++)&#123;</span><br><span class="line">                <span class="comment">//确定第2道位置</span></span><br><span class="line">                <span class="keyword">for</span> (j=i+<span class="number">1</span>;j&lt;=k-<span class="number">1</span>;j++)&#123;</span><br><span class="line">                    temp[sum[k+<span class="number">1</span>] - arr[i]-arr[j]-arr[k]]++;</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(temp[n]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h1 id="第二题-树的替换"><a href="#第二题-树的替换" class="headerlink" title="第二题(树的替换)"></a>第二题(树的替换)</h1><h2 id="思路-2"><a href="#思路-2" class="headerlink" title="思路"></a>思路</h2><ul>
<li>根据给定的路径，遍历原树，寻找要替换的节点；</li>
<li>找到要替换的节点的父节点，将其子节点替换为新树。</li>
<li>根据给定路径，dfs遍历树，将路径节点存在一个list，list中倒数第二个节点即为要找的节点。</li>
</ul>
<h2 id="关键点"><a href="#关键点" class="headerlink" title="关键点"></a>关键点</h2><ul>
<li>根据数组构造二叉树。父子节点关系：i  2 i+1  2I+2</li>
<li>层序遍历二叉树。</li>
<li>根据给定路径搜索对应节点，回溯法。类似于<a target="_blank" rel="noopener" href="https://leetcode-cn.com/problems/binary-tree-paths/">257. 二叉树的所有路径 - 力扣（LeetCode） (leetcode-cn.com)</a></li>
</ul>
<h2 id="代码-3"><a href="#代码-3" class="headerlink" title="代码"></a>代码</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.*;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">Main2</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line">        <span class="type">String</span> <span class="variable">s1</span> <span class="operator">=</span> sc.next();</span><br><span class="line">        String[] arr1 = s1.substring(<span class="number">1</span>,s1.length()-<span class="number">1</span>).split(<span class="string">&quot;,&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">String</span> <span class="variable">s2</span> <span class="operator">=</span> sc.next();</span><br><span class="line">        String[] arr2 = s2.substring(<span class="number">1</span>).split(<span class="string">&quot;/&quot;</span>);</span><br><span class="line">        <span class="type">int</span>[] pathNums = getIntNums(arr2);</span><br><span class="line"></span><br><span class="line">        <span class="type">String</span> <span class="variable">s3</span> <span class="operator">=</span> sc.next();</span><br><span class="line">        String[] arr3 = s3.substring(<span class="number">1</span>,s3.length()-<span class="number">1</span>).split(<span class="string">&quot;,&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="type">TreeNode</span> <span class="variable">fatherTree</span> <span class="operator">=</span> TreeNode.generate(getIntNums(arr1), <span class="number">0</span>);</span><br><span class="line">        <span class="type">TreeNode</span> <span class="variable">sonTree</span> <span class="operator">=</span> TreeNode.generate(getIntNums(arr3), <span class="number">0</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//用来记录原树中的路径节点</span></span><br><span class="line">        Deque&lt;TreeNode&gt; path = <span class="keyword">new</span> <span class="title class_">LinkedList</span>&lt;&gt;();</span><br><span class="line">        getPath(path, fatherTree, pathNums, <span class="number">0</span>);</span><br><span class="line">        <span class="comment">//被替换节点</span></span><br><span class="line">        path.pollLast();</span><br><span class="line">        <span class="comment">//被替换节点的父节点</span></span><br><span class="line">        <span class="type">TreeNode</span> <span class="variable">re</span> <span class="operator">=</span> path.pollLast();</span><br><span class="line">        <span class="comment">//检验被替换节点是被替换节点的左节点还是右节点</span></span><br><span class="line">        <span class="keyword">if</span> (re != <span class="literal">null</span> &amp;&amp; re.left.val == pathNums[pathNums.length-<span class="number">1</span>])&#123;</span><br><span class="line">            re.left = sonTree;</span><br><span class="line">        &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">            re.right = sonTree;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(fatherTree.toString());</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//字符串数组转为int数组</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="type">int</span>[] getIntNums(String[] strs)&#123;</span><br><span class="line">        <span class="type">int</span>[] nus1 = <span class="keyword">new</span> <span class="title class_">int</span>[strs.length];</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>; i&lt;nus1.length; i++)&#123;</span><br><span class="line">            nus1[i] = Integer.valueOf(strs[i]);</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> nus1;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     *</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> path 用来存储路径节点</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> root 原树根节点</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> arr 路径数组</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> index  路径数组索引</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">getPath</span><span class="params">(Deque&lt;TreeNode&gt; path, TreeNode root, <span class="type">int</span>[] arr, <span class="type">int</span> index)</span>&#123;</span><br><span class="line">       <span class="keyword">if</span> (root == <span class="literal">null</span> || path.size() == arr.length || index &gt;= arr.length) <span class="keyword">return</span>;</span><br><span class="line">        <span class="keyword">if</span> (root.val == arr[index])&#123;</span><br><span class="line">            path.offer(root);</span><br><span class="line">            getPath(path, root.left, arr, ++index);</span><br><span class="line">            index--;</span><br><span class="line"></span><br><span class="line">            getPath(path, root.right, arr, ++index);</span><br><span class="line">            index--;</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">class</span> <span class="title class_">TreeNode</span>&#123;</span><br><span class="line">    <span class="type">int</span> val;</span><br><span class="line">    TreeNode left;</span><br><span class="line">    TreeNode right;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">TreeNode</span><span class="params">(<span class="type">int</span> val)</span> &#123;</span><br><span class="line">        <span class="built_in">this</span>.val = val;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">public</span> <span class="title function_">TreeNode</span><span class="params">()</span> &#123; &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//直接重写toString，方便输出树的数组形式</span></span><br><span class="line">    <span class="comment">//层序遍历，输出树的每个节点</span></span><br><span class="line">    <span class="keyword">public</span> String <span class="title function_">toString</span><span class="params">()</span>&#123;</span><br><span class="line">        Queue&lt;TreeNode&gt; queue = <span class="keyword">new</span> <span class="title class_">LinkedList</span>&lt;&gt;();</span><br><span class="line">        queue.offer(<span class="built_in">this</span>);</span><br><span class="line">        <span class="type">StringBuilder</span> <span class="variable">sb</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">StringBuilder</span>();</span><br><span class="line">        <span class="keyword">while</span> (!queue.isEmpty())&#123;</span><br><span class="line">            <span class="type">TreeNode</span> <span class="variable">parent</span> <span class="operator">=</span> queue.poll();</span><br><span class="line">            <span class="comment">//为0的节点不输出</span></span><br><span class="line">            <span class="keyword">if</span> (parent.val!=<span class="number">0</span>) sb.append(parent.val).append(<span class="string">&quot;,&quot;</span>);</span><br><span class="line">            <span class="keyword">if</span>(parent.left!=<span class="literal">null</span>)&#123;</span><br><span class="line">                queue.offer(parent.left);</span><br><span class="line">            &#125;</span><br><span class="line">            <span class="keyword">if</span> (parent.right!=<span class="literal">null</span>)&#123;</span><br><span class="line">                queue.offer(parent.right);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="keyword">return</span> <span class="string">&quot;[&quot;</span>+sb.substring(<span class="number">0</span>,sb.length()-<span class="number">1</span>).toString()+<span class="string">&quot;]&quot;</span>;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//根据数组构建二叉树</span></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> TreeNode <span class="title function_">generate</span><span class="params">(<span class="type">int</span>[] arr, <span class="type">int</span> i)</span>&#123;</span><br><span class="line">        <span class="type">TreeNode</span> <span class="variable">root</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">TreeNode</span>(arr[i]);</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">2</span>*i+<span class="number">1</span>&lt;arr.length) root.left = generate(arr, <span class="number">2</span>*i+<span class="number">1</span>);</span><br><span class="line">        <span class="keyword">if</span> (<span class="number">2</span>*i+<span class="number">2</span>&lt;arr.length) root.right = generate(arr, <span class="number">2</span>*i+<span class="number">2</span>);</span><br><span class="line">        <span class="keyword">return</span> root;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="算例"><a href="#算例" class="headerlink" title="算例"></a>算例</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">[<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">/<span class="number">1</span>/<span class="number">1</span></span><br><span class="line">[<span class="number">5</span>,<span class="number">3</span>,<span class="number">0</span>]</span><br><span class="line">out </span><br><span class="line">[<span class="number">1</span>,<span class="number">5</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line"></span><br><span class="line">[<span class="number">1</span>,<span class="number">1</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">4</span>,<span class="number">5</span>]</span><br><span class="line">/<span class="number">1</span>/<span class="number">2</span></span><br><span class="line">[<span class="number">5</span>,<span class="number">3</span>,<span class="number">0</span>]</span><br><span class="line">out</span><br><span class="line">[<span class="number">1</span>,<span class="number">1</span>,<span class="number">5</span>,<span class="number">3</span>]</span><br><span class="line"></span><br><span class="line">[<span class="number">2</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">6</span>,<span class="number">5</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">3</span>,<span class="number">6</span>,<span class="number">2</span>,<span class="number">7</span>,<span class="number">9</span>,<span class="number">10</span>]</span><br><span class="line">/<span class="number">2</span>/<span class="number">6</span>/<span class="number">6</span>/<span class="number">7</span></span><br><span class="line">[<span class="number">100</span>,<span class="number">40</span>,<span class="number">2</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">0</span>,<span class="number">5</span>]</span><br><span class="line">out</span><br><span class="line">[<span class="number">2</span>,<span class="number">5</span>,<span class="number">6</span>,<span class="number">3</span>,<span class="number">4</span>,<span class="number">6</span>,<span class="number">5</span>,<span class="number">3</span>,<span class="number">6</span>,<span class="number">2</span>,<span class="number">100</span>,<span class="number">9</span>,<span class="number">10</span>,<span class="number">40</span>,<span class="number">2</span>,<span class="number">5</span>]</span><br></pre></td></tr></table></figure>

<h1 id="第三题-1"><a href="#第三题-1" class="headerlink" title="第三题"></a>第三题</h1></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">Sun Moon</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://www.sunmoon.site/2020/01/29/%E6%9C%BA%E8%80%83/%E5%AD%97%E8%8A%82/">https://www.sunmoon.site/2020/01/29/%E6%9C%BA%E8%80%83/%E5%AD%97%E8%8A%82/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://www.sunmoon.site" target="_blank">SunMoon Space</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/%E6%A8%A1%E6%9D%BF/">模板</a></div><div class="post_share"><div class="social-share" data-image="https://s1.ax1x.com/2022/05/19/OHAFsK.md.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><div class="post-reward"><div class="reward-button"><i class="fas fa-qrcode"></i> 打赏</div><div class="reward-main"><ul class="reward-all"><li class="reward-item"><a href="/img/wechat.jpg" target="_blank"><img class="post-qr-code-img" src="/img/wechat.jpg" alt="wechat"/></a><div class="post-qr-code-desc">wechat</div></li><li class="reward-item"><a href="/img/alipay.jpg" target="_blank"><img class="post-qr-code-img" src="/img/alipay.jpg" alt="alipay"/></a><div class="post-qr-code-desc">alipay</div></li></ul></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2021/01/29/%E7%AE%97%E6%B3%95/dfs/"><img class="prev-cover" src="https://s1.ax1x.com/2022/05/19/OHAkqO.md.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">DFS</div></div></a></div><div class="next-post pull-right"><a href="/2020/01/29/%E6%9C%BA%E8%80%83/%E5%A4%A7%E7%96%86/"><img class="next-cover" src="https://s1.ax1x.com/2022/05/19/OHAkqO.md.jpg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">D</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2022/04/29/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91/" title="平衡二叉树"><img class="cover" src="https://s1.ax1x.com/2022/05/19/OHA9R1.md.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-04-29</div><div class="title">平衡二叉树</div></div></a></div><div><a href="/2021/01/29/%E7%AE%97%E6%B3%95/%E5%88%86%E6%B2%BB%E6%80%9D%E6%83%B3/" title="分治思想"><img class="cover" src="https://s1.ax1x.com/2022/05/19/OHAQQP.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-29</div><div class="title">分治思想</div></div></a></div><div><a href="/2021/01/29/%E7%AE%97%E6%B3%95/%E9%80%92%E5%BD%92%E5%9B%9E%E6%BA%AF/" title="递归、回溯"><img class="cover" src="https://s1.ax1x.com/2022/05/19/OHApGR.md.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-29</div><div class="title">递归、回溯</div></div></a></div><div><a href="/2021/01/29/%E7%AE%97%E6%B3%95/bfs/" title="BFS"><img class="cover" src="https://s1.ax1x.com/2022/05/19/OHAQQP.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-29</div><div class="title">BFS</div></div></a></div><div><a href="/2021/01/29/%E7%AE%97%E6%B3%95/dfs/" title="DFS"><img class="cover" src="https://s1.ax1x.com/2022/05/19/OHAkqO.md.jpg" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-01-29</div><div class="title">DFS</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="/img/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Sun Moon</div><div class="author-info__description">Hi, welcome to my blog!</div></div><div class="card-info-data is-center"><div class="card-info-data-item"><a href="/archives/"><div class="headline">文章</div><div class="length-num">43</div></a></div><div class="card-info-data-item"><a href="/tags/"><div class="headline">标签</div><div class="length-num">25</div></a></div><div class="card-info-data-item"><a href="/categories/"><div class="headline">分类</div><div class="length-num">10</div></a></div></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/sun-moon22"><i class="fab fa-github"></i><span>关注我</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/sun-moon22" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://qm.qq.com/cgi-bin/qm/qr?k=zftgTJPReW-aEsUnWx6dKFfD03rovP8a&amp;noverify=0" target="_blank" title="QQ"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">欢迎来到我的博客！</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E9%A2%98"><span class="toc-number">1.</span> <span class="toc-text">第一题</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%9D%E8%B7%AF"><span class="toc-number">1.1.</span> <span class="toc-text">思路</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81"><span class="toc-number">1.2.</span> <span class="toc-text">代码</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%BA%8C%E9%A2%98%EF%BC%88%E8%BF%B7%E5%AE%AB%E9%97%AE%E9%A2%98"><span class="toc-number">2.</span> <span class="toc-text">第二题（迷宫问题</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%9D%E8%B7%AF-1"><span class="toc-number">2.1.</span> <span class="toc-text">思路</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81-1"><span class="toc-number">2.2.</span> <span class="toc-text">代码</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B8%89%E9%A2%98"><span class="toc-number">3.</span> <span class="toc-text">第三题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E5%9B%9B%E9%A2%98"><span class="toc-number">4.</span> <span class="toc-text">第四题</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B8%80%E9%A2%98%EF%BC%88%E5%91%98%E5%B7%A5%E8%80%83%E8%AF%95%EF%BC%89"><span class="toc-number">5.</span> <span class="toc-text">第一题（员工考试）</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%9D%E8%B7%AF%EF%BC%88%E6%9A%B4%E5%8A%9B%E6%90%9C%E7%B4%A2%EF%BC%89"><span class="toc-number">5.1.</span> <span class="toc-text">思路（暴力搜索）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81-2"><span class="toc-number">5.2.</span> <span class="toc-text">代码</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%BA%8C%E9%A2%98-%E6%A0%91%E7%9A%84%E6%9B%BF%E6%8D%A2"><span class="toc-number">6.</span> <span class="toc-text">第二题(树的替换)</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%80%9D%E8%B7%AF-2"><span class="toc-number">6.1.</span> <span class="toc-text">思路</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B3%E9%94%AE%E7%82%B9"><span class="toc-number">6.2.</span> <span class="toc-text">关键点</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81-3"><span class="toc-number">6.3.</span> <span class="toc-text">代码</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%AE%97%E4%BE%8B"><span class="toc-number">6.4.</span> <span class="toc-text">算例</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%AC%AC%E4%B8%89%E9%A2%98-1"><span class="toc-number">7.</span> <span class="toc-text">第三题</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/05/02/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E5%89%8D%E7%BC%80%E6%95%B0/" title="前缀树"><img src="https://s1.ax1x.com/2022/05/19/OHApGR.md.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="前缀树"/></a><div class="content"><a class="title" href="/2022/05/02/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E5%89%8D%E7%BC%80%E6%95%B0/" title="前缀树">前缀树</a><time datetime="2022-05-01T16:37:00.000Z" title="发表于 2022-05-02 00:37:00">2022-05-02</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/04/30/%E4%BD%BF%E7%94%A8%E5%BF%85%E8%AF%BB/" title="Ikiler在线 Markdown"><img src="https://s1.ax1x.com/2022/05/19/OHAkqO.md.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Ikiler在线 Markdown"/></a><div class="content"><a class="title" href="/2022/04/30/%E4%BD%BF%E7%94%A8%E5%BF%85%E8%AF%BB/" title="Ikiler在线 Markdown">Ikiler在线 Markdown</a><time datetime="2022-04-29T16:37:00.000Z" title="发表于 2022-04-30 00:37:00">2022-04-30</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/04/29/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91/" title="平衡二叉树"><img src="https://s1.ax1x.com/2022/05/19/OHA9R1.md.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="平衡二叉树"/></a><div class="content"><a class="title" href="/2022/04/29/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E5%B9%B3%E8%A1%A1%E4%BA%8C%E5%8F%89%E6%A0%91/" title="平衡二叉树">平衡二叉树</a><time datetime="2022-04-28T16:37:00.000Z" title="发表于 2022-04-29 00:37:00">2022-04-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/04/29/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E7%BA%BF%E6%80%A7%E8%A1%A8/" title="线性表"><img src="https://s1.ax1x.com/2022/05/19/OHApGR.md.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="线性表"/></a><div class="content"><a class="title" href="/2022/04/29/%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84/%E7%BA%BF%E6%80%A7%E8%A1%A8/" title="线性表">线性表</a><time datetime="2022-04-28T16:37:00.000Z" title="发表于 2022-04-29 00:37:00">2022-04-29</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/01/29/%E4%BA%92%E8%81%94%E7%BD%91%E7%94%9F%E6%80%81/Docker/" title="docker"><img src="https://s1.ax1x.com/2022/05/19/OHACxx.md.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="docker"/></a><div class="content"><a class="title" href="/2022/01/29/%E4%BA%92%E8%81%94%E7%BD%91%E7%94%9F%E6%80%81/Docker/" title="docker">docker</a><time datetime="2022-01-28T16:37:00.000Z" title="发表于 2022-01-29 00:37:00">2022-01-29</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2023 By Sun Moon</div><div class="footer_custom_text"><a href="https://beian.miit.gov.cn/"  style="color:white" target="_blank">晋ICP备2022004590号-1</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui/dist/fancybox.umd.js"></script><div class="js-pjax"></div><script src="https://widget.qweather.net/simple/static/js/he-simple-common.js?v=2.0"></script><script async src="/js/weather.js"></script><script src="https://myhkw.cn/player/js/jquery.min.js" type="text/javascript"></script><script src="https://myhkw.cn/api/player/1651412271121" id="myhk" key="1651412271121" m="1"></script><script id="canvas_nest" defer="defer" color="0,0,255" opacity="0.7" zIndex="-1" count="99" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-nest.min.js"></script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-show-text.min.js" data-mobile="true" data-text="暴帅,暴富,健康,happy" data-fontsize="15px" data-random="true" async="async"></script><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script><script src="https://cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script><script src="https://cdn.jsdelivr.net/npm/pjax/pjax.min.js"></script><script>let pjaxSelectors = ["title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener scroll 
  window.tocScrollFn && window.removeEventListener('scroll', window.tocScrollFn)
  window.scrollCollect && window.removeEventListener('scroll', scrollCollect)

  typeof preloader === 'object' && preloader.initLoading()
  document.getElementById('rightside').style.cssText = "opacity: ''; transform: ''"
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof chatBtnFn === 'function' && chatBtnFn()
  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()

  typeof preloader === 'object' && preloader.endLoading()
})

document.addEventListener('pjax:error', (e) => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>